package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

public class ReasonCodeOp extends AbstractOperationServicePlugIn {
    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        for (DynamicObject dataEntity : e.getDataEntities()) {
            String name = dataEntity.getDataEntityType().getName();
            if (name.equals("er_dailyloanbill")){
                DynamicObject reasonCode = getReasonCode(dataEntity);
                dataEntity.set("qeug_reasoncode",reasonCode);
            } else if (name.equals("er_tripreimbursebill") || name.equals(" er_publicreimbursebill")){//差旅报销只需要表头成本中心
                DynamicObject reasonCode = getReasonCode(dataEntity);
                dataEntity.set("qeug_reasoncode",reasonCode);
                if (dataEntity.getDynamicObject("qeug_wbs")!=null){
                    QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", "D10")};
                    DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
                    dataEntity.set("qeug_reasoncode",reasoncodeInfo);
                }
            } else if (name.equals("er_dailyreimbursebill")) {
                boolean isbeforeshare = dataEntity.getBoolean("isbeforeshare");//费用报销考虑分摊
                DynamicObjectCollection expenseentryentity = dataEntity.getDynamicObjectCollection("expenseentryentity");
                if (isbeforeshare){//费用分摊
                    BigDecimal decimal = BigDecimal.ZERO;
                    int seq = 1;
                    DynamicObjectCollection expenseentryentityRule = dataEntity.getDynamicObjectCollection("expenseentryentity_rule");
                    for (DynamicObject dynamicObject : expenseentryentityRule) {
                        BigDecimal sharerateComrule = dynamicObject.getBigDecimal("sharerate_comrule");
                        if (sharerateComrule.compareTo(decimal)>=0){
                            decimal=sharerateComrule;
                            seq= dynamicObject.getInt("seq");//分摊取比例最大一行的成本中心
                        }
                    }
                    DynamicObject stdEntrycostcenterCrule = expenseentryentityRule.get(seq-1).getDynamicObject("std_entrycostcenter_crule");
                    if(stdEntrycostcenterCrule!=null){
                        QFilter[] q1 = new QFilter[]{new QFilter("number", "=", stdEntrycostcenterCrule.getString("number"))};
                        DynamicObject costcenterInfo = BusinessDataServiceHelper.loadSingle("bos_costcenter", q1);
                        String qeugReasoncode = costcenterInfo.getString("qeug_reasoncode");
                        String reasoncode ="";
                        if (qeugReasoncode.equals("0010") || qeugReasoncode.equals("0020")){
                            reasoncode = "B42";
                        }else {
                            reasoncode = "B10";
                        }
                        QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", reasoncode)};
                        DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
                        dataEntity.set("qeug_reasoncode",reasoncodeInfo);
                    }
                }else {//没有费用分摊
                    String reasoncode ="";
                    for (DynamicObject dynamicObject : expenseentryentity) {
                        DynamicObject expenseitem = dynamicObject.getDynamicObject("expenseitem");
                        long exid = expenseitem.getLong("id");
                        String exNUm = expenseitem.getString("number");
                        QFilter[] q1 = new QFilter[]{new QFilter("entryentity.basefactor0", "=", exid),
                                new QFilter("number", "=", "08")};
                        DynamicObject accountmaptype = BusinessDataServiceHelper.loadSingle("ai_accountmaptype", q1);
                        if (accountmaptype!=null){
                            DynamicObjectCollection entryentity = accountmaptype.getDynamicObjectCollection("entryentity");
                            for (DynamicObject entry : entryentity) {
//                            long id = entry.getDynamicObject("basefactor0").getLong("id");
                                String bdinfoimport = entry.getString("bdinfoimport");
                                if (bdinfoimport .contains(exNUm)){
                                    DynamicObject accfield = entry.getDynamicObject("accfield");
                                    String accName = accfield.getString("fullname");
                                    if (accName.contains("职工薪")){
                                        reasoncode = "B20";
                                    } else if (accName.contains("诉讼服务")) {
                                        reasoncode = "B42";
                                    }else {
                                        DynamicObject reasonCode = getReasonCode(dataEntity);
                                        reasoncode = reasonCode.getString("number");
                                    }
                                }
                            }
                        }
                    }
                    QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", reasoncode)};
                    DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
                    dataEntity.set("qeug_reasoncode",reasoncodeInfo);
                }
                for (DynamicObject dynamicObject : expenseentryentity) {
                    DynamicObject expenseitem = dynamicObject.getDynamicObject("expenseitem");
                    if(expenseitem!=null){
                        if (expenseitem.getString("number").equals("FYXM0042.06.01")){
                            QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", "B41")};
                            DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
                            dataEntity.set("qeug_reasoncode",reasoncodeInfo);
                        }
                    }
                }
                if (dataEntity.getDynamicObject("qeug_wbs")!=null){
                    QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", "D10")};
                    DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
                    dataEntity.set("qeug_reasoncode",reasoncodeInfo);
                }
            }else {
                DynamicObject reasonCode = getReasonCode(dataEntity);
                dataEntity.set("qeug_reasoncode",reasonCode);
            }
        }
    }

    /**
     * 获取原因代码
     * @param dataEntity
     * @return
     */
    public DynamicObject getReasonCode(DynamicObject dataEntity){
        DynamicObject costcenter = dataEntity.getDynamicObject("std_costcenter");
        if (costcenter!=null){
            QFilter[] q1 = new QFilter[]{new QFilter("number", "=", costcenter.getString("number"))};
            DynamicObject costcenterInfo = BusinessDataServiceHelper.loadSingle("bos_costcenter", q1);
            String qeugReasoncode = costcenterInfo.getString("qeug_reasoncode");
            String reasoncode ="";
            if (qeugReasoncode.equals("0010") || qeugReasoncode.equals("0020")){
                reasoncode = "B42";
            }else {
                reasoncode = "B10";
            }
            QFilter[] qFilte = new QFilter[]{new QFilter("number", "=", reasoncode)};
            DynamicObject reasoncodeInfo = BusinessDataServiceHelper.loadSingle("bos_assistantdata_detail", qFilte);
            return reasoncodeInfo;
        }
        return null;
    }
}
